/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.adapterservice.model;

import com.pingan.UserInfo;
import com.pingan.mobilegw.mobileservice.invoke.MobileRpcHolder;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 无线网关Session
 * 
 * @author jiadao
 * @version $Id: CacheSession.java, v 0.1 2013-4-7 下午2:09:16 jiadao Exp $
 */
public class CacheSession extends ToString {

    /** serialVersionUID */
    private static final long   serialVersionUID = -1064647001765314173L;

    /** 分布式sessionId */
    private String              sessionId;

    /** 会话是否过期 */
    private boolean             expired          = false;

    /** 用户信息 */
    private UserInfo            userInfo;

    /** 操作员信息 */
    private OperatorInfo        operatorInfo;

    /** 登陆信息 */
    private LoginInfo           loginInfo;

    /** 设备信息 */
    private DeviceInfo          deviceInfo;

    /** 客户端信息 */
    private ClientInfo          clientInfo;

    /** 客户端扩展信息（mobilegw专用） */
    private Map<String, Object> extInfos         = new HashMap<String, Object>();

    /** 扩展属性（业务专用） */
    private Map<String, Object> attributes       = new HashMap<String, Object>();

    /** 最后访问时间 */
    private Date                lastAccessTime   = new Date();

    /**
     * 添加session属性
     * 
     * @param key
     * @param value
     */
    public void addAttribute(String key, Object value) {
        attributes.put(key, value);
        Map<String, MapOperation> changeAttributes = MobileRpcHolder.getIncrementalChanges();
        changeAttributes.put(key, new MapOperation(key, value, OperationType.ADD));
    }

    /**
     * 获取session属性
     * 
     * @param key
     * @return
     */
    public Object getAttribute(String key) {
        return attributes.get(key);
    }

    /**
     * 删除session属性
     * 
     * @param key
     * @return
     */
    public Boolean removeAttribute(String key) {
        Object ob = attributes.remove(key);
        boolean isSuccess = ob == null ? false : true;
        if (isSuccess) {
            Map<String, MapOperation> changeAttributes = MobileRpcHolder.getIncrementalChanges();
            changeAttributes.put(key, new MapOperation(key, null, OperationType.DELETE));
        }
        return isSuccess;
    }

    /**
     * 添加mobilegw自有的扩展属性
     * 
     * @param key
     * @param value
     */
    public void addMgwExtInfo(String key, Object value) {
        extInfos.put(key, value);
    }

    /**
     * 获取mobilegw自有的扩展属性
     * 
     * @param key
     * @return
     */
    public Object getMgwExtInfo(String key) {
        return extInfos.get(key);
    }

    /**
     * 删除mobilegw自有的扩展属性
     * 
     * @param key
     * @return
     */
    public Boolean removeMgwExtInfo(String key) {
        Object ob = extInfos.remove(key);
        return ob == null ? false : true;
    }

    /**
     * Getter method for property <tt>deviceInfo</tt>.
     * 
     * @return property value of deviceInfo
     */
    public DeviceInfo getDeviceInfo() {
        return deviceInfo;
    }

    /**
     * Setter method for property <tt>deviceInfo</tt>.
     * 
     * @param deviceInfo value to be assigned to property deviceInfo
     */
    public void setDeviceInfo(DeviceInfo deviceInfo) {
        this.deviceInfo = deviceInfo;
        Map<String, MapOperation> changeAttributes = MobileRpcHolder.getIncrementalChanges();
        changeAttributes.put(MobilegwConstant.DEVICE_CACHE_FLAG, new MapOperation(
            MobilegwConstant.DEVICE_CACHE_FLAG, "add", OperationType.ADD));
    }

    /**
     * Getter method for property <tt>clientInfo</tt>.
     * 
     * @return property value of clientInfo
     */
    public ClientInfo getClientInfo() {
        return clientInfo;
    }

    /**
     * Setter method for property <tt>clientInfo</tt>.
     * 
     * @param clientInfo value to be assigned to property clientInfo
     */
    public void setClientInfo(ClientInfo clientInfo) {
        this.clientInfo = clientInfo;
    }

    /**
     * Getter method for property <tt>extInfos</tt>.
     * 
     * @return property value of extInfos
     */
    public Map<String, Object> getExtInfos() {
        return extInfos;
    }

    /**
     * Setter method for property <tt>extInfos</tt>.
     * 
     * @param extInfos value to be assigned to property extInfos
     */
    public void setExtInfos(Map<String, Object> extInfos) {
        this.extInfos = extInfos;
    }

    /**
     * Getter method for property <tt>loginInfo</tt>.
     * 
     * @return property value of loginInfo
     */
    public LoginInfo getLoginInfo() {
        return loginInfo;
    }

    /**
     * Setter method for property <tt>loginInfo</tt>.
     * 
     * @param loginInfo value to be assigned to property loginInfo
     */
    public void setLoginInfo(LoginInfo loginInfo) {
        this.loginInfo = loginInfo;
    }

    /**
     * Getter method for property <tt>sessionId</tt>.
     * 
     * @return property value of sessionId
     */
    public String getSessionId() {
        return sessionId;
    }

    /**
     * Setter method for property <tt>sessionId</tt>.
     * 
     * @param sessionId value to be assigned to property sessionId
     */
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    /**
     * Getter method for property <tt>expired</tt>.
     * 
     * @return property value of expired
     */
    public boolean isExpired() {
        return expired;
    }

    /**
     * Setter method for property <tt>expired</tt>.
     * 
     * @param expired value to be assigned to property expired
     */
    public void setExpired(boolean expired) {
        this.expired = expired;
    }

    /**
     * Getter method for property <tt>userInfo</tt>.
     * 
     * @return property value of userInfo
     */
    public UserInfo getUserInfo() {
        return userInfo;
    }

    /**
     * Setter method for property <tt>userInfo</tt>.
     * 
     * @param userInfo value to be assigned to property userInfo
     */
    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
        Map<String, MapOperation> changeAttributes = MobileRpcHolder.getIncrementalChanges();
        changeAttributes.put(MobilegwConstant.LOGIN_CACHE_FLAG, new MapOperation(
            MobilegwConstant.LOGIN_CACHE_FLAG, "add", OperationType.ADD));
    }

    /**
     * Getter method for property <tt>attributes</tt>.
     * 
     * @return property value of attributes
     */
    public Map<String, Object> getAttributes() {
        return attributes;
    }

    /**
     * Setter method for property <tt>attributes</tt>.
     * 
     * @param attributes value to be assigned to property attributes
     */
    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

    /**
     * Getter method for property <tt>lastAccessTime</tt>.
     * 
     * @return property value of lastAccessTime
     */
    public Date getLastAccessTime() {
        return lastAccessTime;
    }

    /**
     * Setter method for property <tt>lastAccessTime</tt>.
     * 
     * @param lastAccessTime value to be assigned to property lastAccessTime
     */
    public void setLastAccessTime(Date lastAccessTime) {
        this.lastAccessTime = lastAccessTime;
    }

    public OperatorInfo getOperatorInfo() {
        return operatorInfo;
    }

    public void setOperatorInfo(OperatorInfo operatorInfo) {
        this.operatorInfo = operatorInfo;
    }

}
